package com.yt.user.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.user.models.service.ICustomerPhoneService;
import com.yt.user.models.bo.CustomerPhoneBO;
import com.yt.user.models.dto.CustomerPhoneDTO;

import java.util.List;

/**
 * 客户手机号关联关系控制器
 *
 * @author yzz
 * @Date 2025-10-22 09:43:35
 */

@RestController
@RequestMapping("/api/customerPhone")
@Slf4j
@Api(value = "CustomerPhoneApiController",tags = "客户手机号关联关系Api接口")
public class CustomerPhoneApiController {

    @DubboReference
    private ICustomerPhoneService customerPhoneService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<CustomerPhoneDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        CustomerPhoneDTO dto = customerPhoneService.selectById(id);

        return new Result<>(dto);
    }
    
    @PostMapping("/getByPhoneNumber")
    @ApiOperation(value = "根据手机号查找", notes = "根据手机号查找客户手机号关联关系")
    @ApiImplicitParam(name = "phoneNumber", value = "手机号", required = true, paramType = "query", dataType = "String")
    public Result<List<CustomerPhoneDTO>> getByPhoneNumber(@RequestParam("phoneNumber") String phoneNumber) {
        if (phoneNumber == null || phoneNumber.isEmpty()) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "手机号不能为空");
        }
        
        Wrapper<CustomerPhoneDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("phone", phoneNumber);
        List<CustomerPhoneDTO> dtoList = customerPhoneService.selectList(wrapper);
        
        return new Result<>(dtoList);
    }

    @PostMapping("/getByCustomerId")
    @ApiOperation(value = "根据客户ID查找", notes = "根据客户ID查找客户手机号关联关系")
    @ApiImplicitParam(name = "customerId", value = "客户ID", required = true, paramType = "query", dataType = "String")
    public Result<List<CustomerPhoneDTO>> getByCustomerId(@RequestParam("customerId") String customerId) {
        if (customerId == null || customerId.isEmpty()) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "客户ID不能为空");
        }
        
        Wrapper<CustomerPhoneDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("customer_id", customerId);
        List<CustomerPhoneDTO> dtoList = customerPhoneService.selectList(wrapper);
        
        return new Result<>(dtoList);
    }

    @ApiOperation(value = "客户手机号关联关系列表",notes = "客户手机号关联关系列表")
    @PostMapping("/queryPageList")
    public Result<Page<CustomerPhoneDTO>> queryPageList(CustomerPhoneBO bo){
        Wrapper<CustomerPhoneDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<CustomerPhoneDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = customerPhoneService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}